// 格式化请求图片的大小
export const forMatImageUrl = (
  url: string,
  width: number,
  height: number = width
) => {
  return `${url}?param=${width}y${height}`
}

// 格式化时间 毫秒 =>  05:12
export const forMatTime = (time: number) => {
  if (!time) return '00:00'
  const minutes = Math.floor(time / 60000)
  const seconds = Math.floor((time % 60000) / 1000)
  return `${minutes >= 10 ? minutes : '0' + minutes}:${
    seconds >= 10 ? seconds : '0' + seconds
  }`
}

// 解析歌词
export const parseLyric = (lyric: string) => {
  const lines = lyric.split('\n')
  const result: { time: number; text: string }[] = []
  for (const line of lines) {
    const timeReg = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/g
    const timeResult = timeReg.exec(line)
    if (timeResult) {
      const minutes = Number(timeResult[1]) * 60 *1000
      const seconds = Number(timeResult[2]) * 1000
      const milliseconds = timeResult[3].length === 2 ? Number(timeResult[3]) * 10 : Number(timeResult[3])
      // 返回毫秒
      const time = minutes + seconds + milliseconds
      result.push({ time, text: line.replace(timeReg, '').trim() })
    }
  }
  return result
}
